package com.ibeeking.found.cop.b.rest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ibeeking.found.common.config.prop.FileOssProp;
import com.ibeeking.found.common.utils.UrlUtils;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.cop.b.rest.service.ICopAttachementService;
import com.ibeeking.found.cop.b.rest.service.IWxOpenService;
import com.ibeeking.found.cop.service.common.bo.*;
import com.ibeeking.found.cop.service.common.constants.WxCpMediaFileTypeConstant;
import com.ibeeking.found.cop.service.common.dos.CopAttachementDO;
import com.ibeeking.found.cop.service.common.param.CopAttachementParam;
import com.ibeeking.found.cop.service.common.param.UniAttachParam;
import com.ibeeking.found.cop.service.common.query.CopAttachementQuery;
import com.ibeeking.found.cop.service.common.vo.CopAttachementVO;
import com.ibeeking.found.cop.service.common.vo.UniAttachVO;
import com.ibeeking.found.cop.service.mapper.CopAttachementMapper;
import com.ibeeking.nematos.core.utils.SpringBeanUtil;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.log.utils.LogUtils;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.file.FileUtils;
import com.ibeeking.nematos.utils.id.IdGenerator;
import com.ibeeking.nematos.utils.json.JsonUtils;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.cp.api.WxCpMediaService;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.external.msg.Attachment;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName CopAttachementServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-01-27 18:50
 **/
@Service
public class CopAttachementServiceImpl extends ServiceImpl<CopAttachementMapper, CopAttachementDO> implements ICopAttachementService {

    @Resource
    private CopAttachementMapper copAttachementMapper;

    @Resource
    private IWxOpenService wxOpenServiceImpl;

    @Resource
    private FileOssProp fileOssProp;

    @Override
    public List<CopAttachementVO> list(CopAttachementQuery query) {
        List<CopAttachementDO> entityDOS = copAttachementMapper.selectList(new LambdaQueryWrapper<CopAttachementDO>()
                .eq(query.getId() != null, CopAttachementDO::getId, query.getId())
                .eq(query.getMessageId() != null, CopAttachementDO::getMessageId, query.getMessageId())
                .in(Collections3.isNotEmpty(query.getMessageIds()), CopAttachementDO::getMessageId, query.getMessageId())
                .orderByDesc(CopAttachementDO::getCreateTime)
        );
        List<CopAttachementVO> entityVOS =JsonUtils.jsonToList(entityDOS, CopAttachementVO.class);
        if(Collections3.isNotEmpty(entityVOS)){
            entityVOS.stream().forEach(d->{
                String imageJson = d.getImageJson();
                if(StringUtils.isNotEmpty(imageJson)){
                    List<AttachImageBO> attachImageBOS = JsonUtils.jsonToList(imageJson, AttachImageBO.class);
                    d.setAttachImages(attachImageBOS);
                }
                String linkJson = d.getLinkJson();
                if(StringUtils.isNotEmpty(linkJson)){
                    List<AttachLinkBO> attachLinkBOS = JsonUtils.jsonToList(linkJson, AttachLinkBO.class);
                    d.setAttachLinks(attachLinkBOS);
                }
                String voiceJson = d.getVoiceJson();
                if(StringUtils.isNotEmpty(voiceJson)){
                    List<AttachVoiceBO> attachVoiceBOS = JsonUtils.jsonToList(voiceJson, AttachVoiceBO.class);
                    d.setAttachVoices(attachVoiceBOS);
                }
                String videoJson = d.getVideoJson();
                if(StringUtils.isNotEmpty(videoJson)){
                    List<AttachVideoBO> attachVideoBOS = JsonUtils.jsonToList(videoJson, AttachVideoBO.class);
                    d.setAttachVideos(attachVideoBOS);
                }
                String docJson = d.getDocJson();
                if(StringUtils.isNotEmpty(docJson)){
                    List<AttachDocBO> attachDocBOS = JsonUtils.jsonToList(docJson, AttachDocBO.class);
                    d.setAttachDocs(attachDocBOS);
                }
                String wxmaJson = d.getWxmaJson();
                if(StringUtils.isNotEmpty(wxmaJson)){
                    List<AttachWxmaBO> attachWxmaBOS = JsonUtils.jsonToList(wxmaJson, AttachWxmaBO.class);
                    d.setAttachWxmas(attachWxmaBOS);
                }
                String locationJson = d.getLocationJson();
                if(StringUtils.isNotEmpty(locationJson)){
                    List<AttachLocationBO> attachLocationBOS = JsonUtils.jsonToList(locationJson, AttachLocationBO.class);
                    d.setAttachLocations(attachLocationBOS);
                }
            });
        }
        return entityVOS;
    }

    @Override
    public CopAttachementVO queryByMessageId(Long messageId) {
        CopAttachementQuery query = new CopAttachementQuery();
        query.setMessageId(messageId);
        List<CopAttachementVO> copAttachementVOS = this.getBean().list(query);
        if(Collections3.isNotEmpty(copAttachementVOS)){
            return copAttachementVOS.get(0);
        }else{
            return null;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean add(CopAttachementParam param) {
        CopAttachementDO entityDO = BeanUtil.convertBean(param, CopAttachementDO.class);
        entityDO.setId(IdGenerator.getId());
        List<AttachImageBO> attachImageBOS = param.getAttachImages();
        if(Collections3.isNotEmpty(attachImageBOS)){
            entityDO.setImageJson(JsonUtils.toJsonStr(attachImageBOS));
        }
        List<AttachLinkBO> attachLinkBOS = param.getAttachLinks();
        if(Collections3.isNotEmpty(attachLinkBOS)){
            entityDO.setLinkJson(JsonUtils.toJsonStr(attachLinkBOS));
        }
        List<AttachVoiceBO> attachVoiceBOS = param.getAttachVoices();
        if(Collections3.isNotEmpty(attachVoiceBOS)){
            entityDO.setVoiceJson(JsonUtils.toJsonStr(attachVoiceBOS));
        }
        List<AttachVideoBO> attachVideoBOS = param.getAttachVideos();
        if(Collections3.isNotEmpty(attachVideoBOS)){
            entityDO.setVideoJson(JsonUtils.toJsonStr(attachVideoBOS));
        }
        List<AttachDocBO> attachDocBOS = param.getAttachDocs();
        if(Collections3.isNotEmpty(attachDocBOS)){
            entityDO.setDocJson(JsonUtils.toJsonStr(attachDocBOS));
        }
        List<AttachWxmaBO> attachWxmaBOS = param.getAttachWxmas();
        if(Collections3.isNotEmpty(attachWxmaBOS)){
            entityDO.setWxmaJson(JsonUtils.toJsonStr(attachWxmaBOS));
        }
        List<AttachLocationBO> attachLocationBOS = param.getAttachLocations();
        if(Collections3.isNotEmpty(attachLocationBOS)){
            entityDO.setLocationJson(JsonUtils.toJsonStr(attachLocationBOS));
        }
        entityDO.setCreateBy(UserUtils.getUserId());
        entityDO.setCreateTime(LocalDateTime.now());
        entityDO.setModifyTime(LocalDateTime.now());
        return copAttachementMapper.insert(entityDO) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean batchAdd(List<CopAttachementParam> params) {
        if(Collections3.isNotEmpty(params)){
            List<CopAttachementDO> copAttachementDOS = new ArrayList<>();
            params.stream().forEach(param->{
                CopAttachementDO entityDO = BeanUtil.convertBean(param, CopAttachementDO.class);
                entityDO.setId(IdGenerator.getId());
                List<AttachImageBO> attachImageBOS = param.getAttachImages();
                if(Collections3.isNotEmpty(attachImageBOS)){
                    entityDO.setImageJson(JsonUtils.toJsonStr(attachImageBOS));
                }
                List<AttachLinkBO> attachLinkBOS = param.getAttachLinks();
                if(Collections3.isNotEmpty(attachLinkBOS)){
                    entityDO.setLinkJson(JsonUtils.toJsonStr(attachLinkBOS));
                }
                List<AttachVoiceBO> attachVoiceBOS = param.getAttachVoices();
                if(Collections3.isNotEmpty(attachVoiceBOS)){
                    entityDO.setVoiceJson(JsonUtils.toJsonStr(attachVoiceBOS));
                }
                List<AttachVideoBO> attachVideoBOS = param.getAttachVideos();
                if(Collections3.isNotEmpty(attachVideoBOS)){
                    entityDO.setVideoJson(JsonUtils.toJsonStr(attachVideoBOS));
                }
                List<AttachDocBO> attachDocBOS = param.getAttachDocs();
                if(Collections3.isNotEmpty(attachDocBOS)){
                    entityDO.setDocJson(JsonUtils.toJsonStr(attachDocBOS));
                }
                List<AttachWxmaBO> attachWxmaBOS = param.getAttachWxmas();
                if(Collections3.isNotEmpty(attachWxmaBOS)){
                    entityDO.setWxmaJson(JsonUtils.toJsonStr(attachWxmaBOS));
                }
                List<AttachLocationBO> attachLocationBOS = param.getAttachLocations();
                if(Collections3.isNotEmpty(attachLocationBOS)){
                    entityDO.setLocationJson(JsonUtils.toJsonStr(attachLocationBOS));
                }
                entityDO.setCreateBy(UserUtils.getUserId());
                entityDO.setCreateTime(LocalDateTime.now());
                entityDO.setModifyTime(LocalDateTime.now());
                copAttachementDOS.add(entityDO);
            });
            return copAttachementMapper.insertBatchSomeColumn(copAttachementDOS) >0;
        }else{
            return false;
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean modify(CopAttachementParam param) {
        CopAttachementDO entityDO = BeanUtil.convertBean(param, CopAttachementDO.class);
        List<AttachImageBO> attachImageBOS = param.getAttachImages();
        if(Collections3.isNotEmpty(attachImageBOS)){
            entityDO.setImageJson(JsonUtils.toJsonStr(attachImageBOS));
        }
        List<AttachLinkBO> attachLinkBOS = param.getAttachLinks();
        if(Collections3.isNotEmpty(attachLinkBOS)){
            entityDO.setLinkJson(JsonUtils.toJsonStr(attachLinkBOS));
        }
        List<AttachVoiceBO> attachVoiceBOS = param.getAttachVoices();
        if(Collections3.isNotEmpty(attachVoiceBOS)){
            entityDO.setVoiceJson(JsonUtils.toJsonStr(attachVoiceBOS));
        }
        List<AttachVideoBO> attachVideoBOS = param.getAttachVideos();
        if(Collections3.isNotEmpty(attachVideoBOS)){
            entityDO.setVideoJson(JsonUtils.toJsonStr(attachVideoBOS));
        }
        List<AttachDocBO> attachDocBOS = param.getAttachDocs();
        if(Collections3.isNotEmpty(attachDocBOS)){
            entityDO.setDocJson(JsonUtils.toJsonStr(attachDocBOS));
        }
        List<AttachWxmaBO> attachWxmaBOS = param.getAttachWxmas();
        if(Collections3.isNotEmpty(attachWxmaBOS)){
            entityDO.setWxmaJson(JsonUtils.toJsonStr(attachWxmaBOS));
        }
        List<AttachLocationBO> attachLocationBOS = param.getAttachLocations();
        if(Collections3.isNotEmpty(attachLocationBOS)){
            entityDO.setLocationJson(JsonUtils.toJsonStr(attachLocationBOS));
        }
        entityDO.setModifyBy(UserUtils.getUserId());
        entityDO.setModifyTime(LocalDateTime.now());
        return copAttachementMapper.updateById(entityDO) > 0;
    }

    @Override
    public CopAttachementVO queryById(Long id) {
        CopAttachementDO entityDO = copAttachementMapper.selectById(id);
        if(entityDO == null) throw new BusinessException("没有查到相关记录！");
        CopAttachementVO entityVO = BeanUtil.convertBean(entityDO, CopAttachementVO.class);
        if(entityVO!=null){
            String imageJson = entityVO.getImageJson();
            if(StringUtils.isNotEmpty(imageJson)){
                List<AttachImageBO> attachImageBOS = JsonUtils.jsonToList(imageJson, AttachImageBO.class);
                entityVO.setAttachImages(attachImageBOS);
            }
            String linkJson = entityVO.getLinkJson();
            if(StringUtils.isNotEmpty(linkJson)){
                List<AttachLinkBO> attachLinkBOS = JsonUtils.jsonToList(linkJson, AttachLinkBO.class);
                entityVO.setAttachLinks(attachLinkBOS);
            }
            String voiceJson = entityVO.getVoiceJson();
            if(StringUtils.isNotEmpty(voiceJson)){
                List<AttachVoiceBO> attachVoiceBOS = JsonUtils.jsonToList(voiceJson, AttachVoiceBO.class);
                entityVO.setAttachVoices(attachVoiceBOS);
            }
            String videoJson = entityVO.getVideoJson();
            if(StringUtils.isNotEmpty(videoJson)){
                List<AttachVideoBO> attachVideoBOS = JsonUtils.jsonToList(videoJson, AttachVideoBO.class);
                entityVO.setAttachVideos(attachVideoBOS);
            }
            String docJson = entityVO.getDocJson();
            if(StringUtils.isNotEmpty(docJson)){
                List<AttachDocBO> attachDocBOS = JsonUtils.jsonToList(docJson, AttachDocBO.class);
                entityVO.setAttachDocs(attachDocBOS);
            }
            String wxmaJson = entityVO.getWxmaJson();
            if(StringUtils.isNotEmpty(wxmaJson)){
                List<AttachWxmaBO> attachWxmaBOS = JsonUtils.jsonToList(wxmaJson, AttachWxmaBO.class);
                entityVO.setAttachWxmas(attachWxmaBOS);
            }
            String locationJson = entityVO.getLocationJson();
            if(StringUtils.isNotEmpty(locationJson)){
                List<AttachLocationBO> attachLocationBOS = JsonUtils.jsonToList(locationJson, AttachLocationBO.class);
                entityVO.setAttachLocations(attachLocationBOS);
            }
        }
        return entityVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean del(Long id) {
        CopAttachementDO entityDO = copAttachementMapper.selectById(id);
        if(entityDO == null) throw new BusinessException("没有查到相关记录！");
        return copAttachementMapper.deleteById(id) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean delByMessageId(Long messageId) {
        int rows = copAttachementMapper.delete(new LambdaQueryWrapper<CopAttachementDO>().eq(CopAttachementDO::getMessageId, messageId));
        return rows >= 0;
    }
    /***
     * 消息对象转换
     */
    @Override
    public CopAttachementParam transerUniAttachementToCopAttachement(List<UniAttachParam> attachementParamS) {
        if(Collections3.isNotEmpty(attachementParamS)){
            CopAttachementParam attachementParam = new CopAttachementParam();
            List<AttachImageBO> attachImageBOS = new ArrayList<>();
            List<AttachVoiceBO> attachVoiceBOS = new ArrayList<>();
            List<AttachVideoBO> attachVideoBOS = new ArrayList<>();
            List<AttachDocBO> attachDocBOS = new ArrayList<>();
            List<AttachLinkBO> attachLinkBOS = new ArrayList<>();
            List<AttachWxmaBO> attachWxmaBOS = new ArrayList<>();
            List<AttachLocationBO> attachLocationBOS = new ArrayList<>();
            for(UniAttachParam attachParam:attachementParamS){
                String msgType = attachParam.getMsgType();
                if(msgType.equalsIgnoreCase(WxCpMediaFileTypeConstant.WXCP_MEDIA_FILE_TYPE_IMAGE)){
                    AttachImageBO attachImageBO = new AttachImageBO();
                    attachImageBO.setMsgType(msgType);
                    attachImageBO.setFileName(attachParam.getFileName());
                    if(StringUtils.isNotEmpty(attachParam.getFilePath())) {
                        attachImageBO.setUrlOfOss(UrlUtils.removeMallUrlDomainName(fileOssProp.getHost(),attachParam.getFilePath()));
                    }
                    attachImageBOS.add(attachImageBO);
                }
                if(msgType.equalsIgnoreCase(WxCpMediaFileTypeConstant.WXCP_MEDIA_FILE_TYPE_VOICE)){
                    AttachVoiceBO attachVoiceBO = new AttachVoiceBO();
                    attachVoiceBO.setMsgType(msgType);
                    attachVoiceBO.setFileName(attachParam.getFileName());
                    attachVoiceBO.setTitle(StringUtils.isNotEmpty(attachParam.getFileName())?FileUtils.getNameNotSuffix(attachParam.getFileName()):"");
                    if(StringUtils.isNotEmpty(attachParam.getFilePath())) {
                        attachVoiceBO.setUrlOfOss(UrlUtils.removeMallUrlDomainName(fileOssProp.getHost(),attachParam.getFilePath()));
                    }
                    attachVoiceBOS.add(attachVoiceBO);
                }
                if(msgType.equalsIgnoreCase(WxCpMediaFileTypeConstant.WXCP_MEDIA_FILE_TYPE_VIDEO)){
                    AttachVideoBO attachVideoBO = new AttachVideoBO();
                    attachVideoBO.setMsgType(msgType);
                    attachVideoBO.setFileName(attachParam.getFileName());
                    attachVideoBO.setTitle(StringUtils.isNotEmpty(attachParam.getFileName())?FileUtils.getNameNotSuffix(attachParam.getFileName()):"");
                    if(StringUtils.isNotEmpty(attachParam.getFilePath())) {
                        attachVideoBO.setUrlOfOss(UrlUtils.removeMallUrlDomainName(fileOssProp.getHost(),attachParam.getFilePath()));
                    }
                    attachVideoBOS.add(attachVideoBO);
                }
                if(msgType.equalsIgnoreCase(WxCpMediaFileTypeConstant.WXCP_MEDIA_FILE_TYPE_FILE)){
                    AttachDocBO attachDocBO = new AttachDocBO();
                    attachDocBO.setMsgType(msgType);
                    attachDocBO.setFileName(attachParam.getFileName());
                    attachDocBO.setTitle(StringUtils.isNotEmpty(attachParam.getFileName())?FileUtils.getNameNotSuffix(attachParam.getFileName()):"");
                    if(StringUtils.isNotEmpty(attachParam.getFilePath())) {
                        attachDocBO.setUrlOfOss(UrlUtils.removeMallUrlDomainName(fileOssProp.getHost(),attachParam.getFilePath()));
                    }
                    attachDocBOS.add(attachDocBO);
                }
                if(msgType.equalsIgnoreCase(WxCpMediaFileTypeConstant.WXCP_MEDIA_FILE_TYPE_LINK)){
                    AttachLinkBO attachLinkBO = new AttachLinkBO();
                    attachLinkBO.setMsgType(msgType);
                    attachLinkBO.setFileName(attachParam.getFileName());
                    attachLinkBO.setTitle(attachParam.getFileName());
                    if(StringUtils.isNotEmpty(attachParam.getFilePath())) {
                        attachLinkBO.setUrlOfOss(UrlUtils.removeMallUrlDomainName(fileOssProp.getHost(),attachParam.getFilePath()));
                    }
                    attachLinkBOS.add(attachLinkBO);
                }
                if(msgType.equalsIgnoreCase(WxCpMediaFileTypeConstant.WXCP_MEDIA_FILE_TYPE_WXMA)){
                    AttachWxmaBO attachWxmaBO = new AttachWxmaBO();
                    attachWxmaBO.setMsgType(msgType);
                    attachWxmaBO.setTitle(attachParam.getTitle());
                    attachWxmaBOS.add(attachWxmaBO);
                }
                if(msgType.equalsIgnoreCase(WxCpMediaFileTypeConstant.WXCP_MEDIA_FILE_TYPE_LOCATION)){
                    AttachLocationBO attachLocationBO = new AttachLocationBO();
                    attachLocationBO.setMsgType(msgType);
                    attachLocationBOS.add(attachLocationBO);
                }
            }
            if(Collections3.isNotEmpty(attachImageBOS)){
                attachementParam.setAttachImages(attachImageBOS);
            }
            if(Collections3.isNotEmpty(attachVoiceBOS)){
                attachementParam.setAttachVoices(attachVoiceBOS);
            }
            if(Collections3.isNotEmpty(attachVideoBOS)){
                attachementParam.setAttachVideos(attachVideoBOS);
            }
            if(Collections3.isNotEmpty(attachDocBOS)){
                attachementParam.setAttachDocs(attachDocBOS);
            }
            if(Collections3.isNotEmpty(attachLinkBOS)){
                attachementParam.setAttachLinks(attachLinkBOS);
            }
            if(Collections3.isNotEmpty(attachWxmaBOS)){
                attachementParam.setAttachWxmas(attachWxmaBOS);
            }
            if(Collections3.isNotEmpty(attachLocationBOS)){
                attachementParam.setAttachLocations(attachLocationBOS);
            }
            return attachementParam;
        }else{
            return null;
        }
    }
    /***
     * 消息对象转换
     */
    @Override
    public List<UniAttachVO> transerCopAttachementToUniAttachement(CopAttachementVO attachementVO) {
        if(null != attachementVO){
            List<UniAttachVO> uniAttachVOS = new ArrayList<>();
            List<AttachImageBO>  attachImageBOS =  attachementVO.getAttachImages();
            List<AttachVoiceBO>  attachVoiceBOS =  attachementVO.getAttachVoices();
            List<AttachVideoBO>  attachVideoBOS =  attachementVO.getAttachVideos();
            List<AttachDocBO>  attachDocBOS =  attachementVO.getAttachDocs();
            List<AttachLinkBO>  attachLinkBOS =  attachementVO.getAttachLinks();
            List<AttachWxmaBO>  attachWxmaBOS =  attachementVO.getAttachWxmas();
            List<AttachLocationBO>  attachLocationBOS =  attachementVO.getAttachLocations();
            if(Collections3.isNotEmpty(attachImageBOS)){
               for(AttachImageBO attachBO:attachImageBOS){
                   UniAttachVO uniAttachVO = new UniAttachVO();
                   uniAttachVO.setMsgType(attachBO.getMsgType());
                   uniAttachVO.setFileName(attachBO.getFileName());
                   String filePathOfOss = attachBO.getUrlOfOss();
                   if(StringUtils.isNotEmpty(filePathOfOss)){
                       String fileFullUrlOfOss = UrlUtils.addUrlDomainName(fileOssProp.getHost(),filePathOfOss);
                       uniAttachVO.setFilePath(fileFullUrlOfOss);
                   }
                   uniAttachVOS.add(uniAttachVO);
               }
            }
            if(Collections3.isNotEmpty(attachVoiceBOS)){
                for(AttachVoiceBO attachBO:attachVoiceBOS){
                    UniAttachVO uniAttachVO = new UniAttachVO();
                    uniAttachVO.setMsgType(attachBO.getMsgType());
                    uniAttachVO.setTitle(attachBO.getTitle());
                    uniAttachVO.setFileName(attachBO.getFileName());
                    String filePathOfOss = attachBO.getUrlOfOss();
                    if(StringUtils.isNotEmpty(filePathOfOss)){
                        String fileFullUrlOfOss = UrlUtils.addUrlDomainName(fileOssProp.getHost(),filePathOfOss);
                        uniAttachVO.setFilePath(fileFullUrlOfOss);
                    }
                    uniAttachVOS.add(uniAttachVO);
                }
            }
            if(Collections3.isNotEmpty(attachVideoBOS)){
                for(AttachVideoBO attachBO:attachVideoBOS){
                    UniAttachVO uniAttachVO = new UniAttachVO();
                    uniAttachVO.setMsgType(attachBO.getMsgType());
                    uniAttachVO.setTitle(attachBO.getTitle());
                    uniAttachVO.setFileName(attachBO.getFileName());
                    String filePathOfOss = attachBO.getUrlOfOss();
                    if(StringUtils.isNotEmpty(filePathOfOss)){
                        String fileFullUrlOfOss = UrlUtils.addUrlDomainName(fileOssProp.getHost(),filePathOfOss);
                        uniAttachVO.setFilePath(fileFullUrlOfOss);
                    }
                    uniAttachVOS.add(uniAttachVO);
                }
            }
            if(Collections3.isNotEmpty(attachDocBOS)){
                for(AttachDocBO attachBO:attachDocBOS){
                    UniAttachVO uniAttachVO = new UniAttachVO();
                    uniAttachVO.setMsgType(attachBO.getMsgType());
                    uniAttachVO.setTitle(attachBO.getTitle());
                    uniAttachVO.setFileName(attachBO.getFileName());
                    String filePathOfOss = attachBO.getUrlOfOss();
                    if(StringUtils.isNotEmpty(filePathOfOss)){
                        String fileFullUrlOfOss = UrlUtils.addUrlDomainName(fileOssProp.getHost(),filePathOfOss);
                        uniAttachVO.setFilePath(fileFullUrlOfOss);
                    }
                    uniAttachVOS.add(uniAttachVO);
                }
            }
            if(Collections3.isNotEmpty(attachLinkBOS)){
                for(AttachLinkBO attachBO:attachLinkBOS){
                    UniAttachVO uniAttachVO = new UniAttachVO();
                    uniAttachVO.setMsgType(attachBO.getMsgType());
                    uniAttachVO.setTitle(attachBO.getTitle());
                    uniAttachVO.setFileName(attachBO.getFileName());
                    String filePathOfOss = attachBO.getUrlOfOss();
                    if(StringUtils.isNotEmpty(filePathOfOss)){
                        String fileFullUrlOfOss = UrlUtils.addUrlDomainName(fileOssProp.getHost(),filePathOfOss);
                        uniAttachVO.setFilePath(fileFullUrlOfOss);
                    }
                    uniAttachVOS.add(uniAttachVO);
                }
            }
            if(Collections3.isNotEmpty(attachWxmaBOS)){
                for(AttachWxmaBO attachBO:attachWxmaBOS){
                    UniAttachVO uniAttachVO = new UniAttachVO();
                    uniAttachVO.setMsgType(attachBO.getMsgType());
                    uniAttachVO.setTitle(attachBO.getTitle());
                    uniAttachVOS.add(uniAttachVO);
                }
            }
            if(Collections3.isNotEmpty(attachLocationBOS)){
                for(AttachLocationBO attachBO:attachLocationBOS){
                    UniAttachVO uniAttachVO = new UniAttachVO();
                    uniAttachVO.setMsgType(attachBO.getMsgType());
                    uniAttachVOS.add(uniAttachVO);
                }
            }
            return uniAttachVOS;
        }else{
            return Collections3.emptyList();
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean syncMessageAttachementsToWxCp(Long messageId){
        Long tenantId = UserUtils.getTenantID();
        CopAttachementVO entityVO = this.getBean().queryByMessageId(messageId);
        LogUtils.info("+++++++++CopAttachementVO:{}+++++++++", JsonUtils.toJsonStr(entityVO));
        if(entityVO != null) {
            CopAttachementDO copAttachementDO = new CopAttachementDO();
            copAttachementDO.setId(entityVO.getId());
            copAttachementDO.setMessageId(messageId);
            try {
                WxCpService wxCpService = wxOpenServiceImpl.getWxCpService(tenantId);
                WxCpConfigStorage wxCpConfigStorage = wxCpService.getWxCpConfigStorage();
                if (wxCpConfigStorage == null) {
                    throw new BusinessException("获取企微服务失败!");
                }
                WxCpMediaService wxCpMediaService = wxCpService.getMediaService();
                if (wxCpMediaService == null) throw new BusinessException("获取媒体自研服务出错!");
                List<AttachImageBO> attachImageBOS = entityVO.getAttachImages();
                if(Collections3.isNotEmpty(attachImageBOS)) {
                    for(AttachImageBO imageBO:attachImageBOS) {
                        String imgOssShortUrl = imageBO.getUrlOfOss();
                        if (StringUtils.isNotEmpty(imgOssShortUrl)) {
                            String imgOssFullUrl = UrlUtils.addUrlDomainName(fileOssProp.getHost(), imgOssShortUrl);
                            File imgFileName = FileUtils.convertUrlToFile(imgOssFullUrl);
                            String imgWxcpFullUrl = wxCpMediaService.uploadImg(imgFileName);
                            LogUtils.info("+++++++++imageBO imgWxcpFullUrl:{}+++++++++", imgWxcpFullUrl);
                            if (StringUtils.isNotEmpty(imgWxcpFullUrl)) {
                                imageBO.setUrlOfWxcp(imgWxcpFullUrl);
                            }
                        }
                    }
                    copAttachementDO.setImageJson(JsonUtils.toJsonStr(attachImageBOS));
                }
                List<AttachVoiceBO> attachVoiceBOS = entityVO.getAttachVoices();
                if(Collections3.isNotEmpty(attachVoiceBOS)) {
                    for(AttachVoiceBO voiceBO:attachVoiceBOS) {
                        String voiceOssShortUrl = voiceBO.getUrlOfOss();
                        if (StringUtils.isNotEmpty(voiceOssShortUrl)) {
                            String imgOssFullUrl = UrlUtils.addUrlDomainName(fileOssProp.getHost(), voiceOssShortUrl);
                            String imgFileName = FileUtils.getUrlFileName(imgOssFullUrl);
                            WxMediaUploadResult WxMediaUploadResult = wxCpMediaService.upload(WxCpMediaFileTypeConstant.WXCP_MEDIA_FILE_TYPE_VOICE, imgFileName, imgOssFullUrl);
                            LogUtils.info("+++++++++imageBO WxMediaUploadResult:{}+++++++++", JsonUtils.toJsonStr(WxMediaUploadResult));
                            if (WxMediaUploadResult != null) {
                                voiceBO.setMediaId(WxMediaUploadResult.getMediaId());
                                voiceBO.setUrlOfWxcp(WxMediaUploadResult.getUrl());
                            }
                        }
                    }
                    copAttachementDO.setVoiceJson(JsonUtils.toJsonStr(attachVoiceBOS));
                }
                List<AttachVideoBO> attachVideoBOS = entityVO.getAttachVideos();
                if(Collections3.isNotEmpty(attachVideoBOS)) {
                    for(AttachVideoBO videoBO:attachVideoBOS) {
                        String videoOssShortUrl = videoBO.getUrlOfOss();
                        if (StringUtils.isNotEmpty(videoOssShortUrl)) {
                            String imgOssFullUrl = UrlUtils.addUrlDomainName(fileOssProp.getHost(), videoOssShortUrl);
                            String imgFileName = FileUtils.getUrlFileName(imgOssFullUrl);
                            WxMediaUploadResult WxMediaUploadResult = wxCpMediaService.upload(WxCpMediaFileTypeConstant.WXCP_MEDIA_FILE_TYPE_VIDEO, imgFileName, imgOssFullUrl);
                            if (WxMediaUploadResult != null) {
                                videoBO.setMediaId(WxMediaUploadResult.getMediaId());
                                videoBO.setUrlOfWxcp(WxMediaUploadResult.getUrl());
                            }
                        }
                    }
                    copAttachementDO.setVideoJson(JsonUtils.toJsonStr(attachVideoBOS));
                }
                List<AttachDocBO> attachDocBOS = entityVO.getAttachDocs();
                if(Collections3.isNotEmpty(attachDocBOS)) {
                    for(AttachDocBO docBO:attachDocBOS) {
                        String fileOssShortUrl = docBO.getUrlOfOss();
                        if (StringUtils.isNotEmpty(fileOssShortUrl)) {
                            String imgOssFullUrl = UrlUtils.addUrlDomainName(fileOssProp.getHost(), fileOssShortUrl);
                            String imgFileName = FileUtils.getUrlFileName(imgOssFullUrl);
                            WxMediaUploadResult WxMediaUploadResult = wxCpMediaService.upload(WxCpMediaFileTypeConstant.WXCP_MEDIA_FILE_TYPE_FILE, imgFileName, imgOssFullUrl);
                            LogUtils.info("+++++++++docBO WxMediaUploadResult:{}+++++++++", JsonUtils.toJsonStr(WxMediaUploadResult));
                            if (WxMediaUploadResult != null) {
                                docBO.setMediaId(WxMediaUploadResult.getMediaId());
                                docBO.setUrlOfWxcp(WxMediaUploadResult.getUrl());
                            }
                        }
                    }
                    copAttachementDO.setDocJson(JsonUtils.toJsonStr(attachDocBOS));
                }
                List<AttachLinkBO> attachLinkBOS = entityVO.getAttachLinks();
                if(Collections3.isNotEmpty(attachLinkBOS)) {
                    for(AttachLinkBO linkBO:attachLinkBOS) {
                        String urlOfOss = linkBO.getUrlOfOss();
                        if (StringUtils.isNotEmpty(urlOfOss)) {
                            String imgOssFullUrl = UrlUtils.addUrlDomainName(fileOssProp.getHost(), urlOfOss);
                            String imgFileName = FileUtils.getUrlFileName(imgOssFullUrl);
                            WxMediaUploadResult WxMediaUploadResult = wxCpMediaService.upload(WxCpMediaFileTypeConstant.WXCP_MEDIA_FILE_TYPE_LINK, imgFileName, imgOssFullUrl);
                            LogUtils.info("+++++++++linkBO WxMediaUploadResult:{}+++++++++", JsonUtils.toJsonStr(WxMediaUploadResult));
                            if (WxMediaUploadResult != null) {
                                linkBO.setMediaId(WxMediaUploadResult.getMediaId());
                                linkBO.setUrlOfWxcp(WxMediaUploadResult.getUrl());
                            }
                        }
                        String urlOfCoverInOss = linkBO.getUrlOfCoverInOss();
                        if (StringUtils.isNotEmpty(urlOfCoverInOss)) {
                            String imgOssFullUrl = UrlUtils.addUrlDomainName(fileOssProp.getHost(), urlOfCoverInOss);
                            String imgFileName = FileUtils.getUrlFileName(imgOssFullUrl);
                            WxMediaUploadResult WxMediaUploadResult = wxCpMediaService.upload(WxCpMediaFileTypeConstant.WXCP_MEDIA_FILE_TYPE_IMAGE, imgFileName, imgOssFullUrl);
                            LogUtils.info("+++++++++linkCoverBO WxMediaUploadResult:{}+++++++++", JsonUtils.toJsonStr(WxMediaUploadResult));
                            if (WxMediaUploadResult != null) {
                                linkBO.setMediaIdOfCover(WxMediaUploadResult.getMediaId());
                                linkBO.setUrlOfCoverInWxcp(WxMediaUploadResult.getUrl());
                            }
                        }
                    }
                    copAttachementDO.setLinkJson(JsonUtils.toJsonStr(attachLinkBOS));
                }
            } catch (Exception ex) {
                LogUtils.error("部分同步上传附件到企微失败,原因：{}", ex);
                return false;
            }
            LogUtils.info("+++++++++copAttachementDO:{}+++++++++", JsonUtils.toJsonStr(copAttachementDO));
            if(copAttachementDO!=null){
                copAttachementMapper.updateById(copAttachementDO);
            }
        }
        return true;
    }

    @Override
    public List<Attachment> transerCopAttachementToWxcpAttachement(List<CopAttachementVO> attachementVOS){
        if(Collections3.isNotEmpty(attachementVOS)) {
            List<Attachment> attachementList = new ArrayList<>();
            //先把消息中的附件同步到企微
            for(CopAttachementVO copAttachementVO: attachementVOS){
                Attachment attachement = new Attachment();
                List<AttachImageBO> attachImageBOS = copAttachementVO.getAttachImages();
                if(Collections3.isNotEmpty(attachImageBOS)){
                    AttachImageBO attachImageBO = attachImageBOS.get(0);
                    if(StringUtils.isNotEmpty(attachImageBO.getUrlOfWxcp())) {
                        me.chanjar.weixin.cp.bean.external.msg.Image image = new me.chanjar.weixin.cp.bean.external.msg.Image();
                        image.setPicUrl(attachImageBO.getUrlOfWxcp());
                        attachement.setImage(image);
                        attachement.setMsgType(WxCpMediaFileTypeConstant.WXCP_MEDIA_FILE_TYPE_IMAGE);
                        attachementList.add(attachement);
                    }
                }
                List<AttachVoiceBO> attachVoiceBOS = copAttachementVO.getAttachVoices();
                if(Collections3.isNotEmpty(attachVoiceBOS)){
                    AttachVoiceBO attachVoiceBO = attachVoiceBOS.get(0);
                    if(StringUtils.isNotEmpty(attachVoiceBO.getMediaId())) {
                        me.chanjar.weixin.cp.bean.external.msg.Video video = new me.chanjar.weixin.cp.bean.external.msg.Video();
                        video.setMediaId(attachVoiceBO.getMediaId());
                        attachement.setVideo(video);
                        attachement.setMsgType(WxCpMediaFileTypeConstant.WXCP_MEDIA_FILE_TYPE_VIDEO);
                        attachementList.add(attachement);
                    }
                }
                List<AttachVideoBO> attachVideoBOS = copAttachementVO.getAttachVideos();
                if(Collections3.isNotEmpty(attachVideoBOS)){
                    AttachVideoBO attachVideoBO = attachVideoBOS.get(0);
                    if(StringUtils.isNotEmpty(attachVideoBO.getMediaId())) {
                        me.chanjar.weixin.cp.bean.external.msg.Video video = new me.chanjar.weixin.cp.bean.external.msg.Video();
                        video.setMediaId(attachVideoBO.getMediaId());
                        video.setThumbMediaId(attachVideoBO.getMediaId());
                        attachement.setVideo(video);
                        attachement.setMsgType(WxCpMediaFileTypeConstant.WXCP_MEDIA_FILE_TYPE_VIDEO);
                        attachementList.add(attachement);
                    }
                }
                List<AttachDocBO> attachDocBOS = copAttachementVO.getAttachDocs();
                if(Collections3.isNotEmpty(attachDocBOS)){
                    AttachDocBO attachDocBO = attachDocBOS.get(0);
                    if(StringUtils.isNotEmpty(attachDocBO.getMediaId())) {
                        me.chanjar.weixin.cp.bean.external.msg.File file = new me.chanjar.weixin.cp.bean.external.msg.File();
                        file.setMediaId(attachDocBO.getMediaId());
                        attachement.setFile(file);
                        attachement.setMsgType(WxCpMediaFileTypeConstant.WXCP_MEDIA_FILE_TYPE_FILE);
                        attachementList.add(attachement);
                    }
                }
                List<AttachLinkBO> attachLinkBOS = copAttachementVO.getAttachLinks();
                if(Collections3.isNotEmpty(attachLinkBOS)){
                    AttachLinkBO attachLinkBO = attachLinkBOS.get(0);
                    if(StringUtils.isNotEmpty(attachLinkBO.getMediaId())) {
                        me.chanjar.weixin.cp.bean.external.msg.Link link = new me.chanjar.weixin.cp.bean.external.msg.Link();
                        link.setMediaId(attachLinkBO.getMediaId());
                        link.setTitle(attachLinkBO.getTitle());
                        link.setPicUrl(attachLinkBO.getUrlOfCoverInWxcp());
                        link.setUrl(attachLinkBO.getUrlOfWxcp());
                        link.setDesc(attachLinkBO.getDesc());
                        attachement.setLink(link);
                        attachement.setMsgType(WxCpMediaFileTypeConstant.WXCP_MEDIA_FILE_TYPE_LINK);
                        attachementList.add(attachement);
                    }
                }
                List<AttachWxmaBO> attachWxmaBOS = copAttachementVO.getAttachWxmas();
                if(Collections3.isNotEmpty(attachWxmaBOS)){
                    AttachWxmaBO attachWxmaBO = attachWxmaBOS.get(0);
                    if(StringUtils.isNotEmpty(attachWxmaBO.getMediaIdOfCover())) {
                        me.chanjar.weixin.cp.bean.external.msg.MiniProgram wxma = new me.chanjar.weixin.cp.bean.external.msg.MiniProgram();
                        wxma.setAppid(attachWxmaBO.getAppid());
                        wxma.setTitle(attachWxmaBO.getTitle());
                        wxma.setPage(attachWxmaBO.getPagePath());
                        wxma.setPicMediaId(attachWxmaBO.getMediaIdOfCover());
                        attachement.setMiniProgram(wxma);
                        attachement.setMsgType(WxCpMediaFileTypeConstant.WXCP_MEDIA_FILE_TYPE_WXMA);
                        attachementList.add(attachement);
                    }
                }
            }
            return attachementList;
        }else{
            return null;
        }
    }

    private ICopAttachementService getBean(){
        return SpringBeanUtil.getBean(CopAttachementServiceImpl.class);
    }
}
